package com.chhuang.myplayer.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.debug
import org.jetbrains.anko.error
import org.jetbrains.anko.info
import org.jetbrains.anko.support.v4.runOnUiThread
import org.jetbrains.anko.support.v4.toast

/**
 * Fragment基类
 */
abstract class BaseFragment : Fragment(), AnkoLogger {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
        info { "onCreate" }
        debug { "onCreate" }
        error { "onCreate" }
    }

    /**
     * Fragment初始化
     */
    open protected fun init() {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return initView()
    }

    /**
     * 初始化布局
     */
    abstract fun initView(): View?

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initListener()
        initData()
    }

    /**
     * 初始化数据
     */
    protected open fun initData() {
    }

    /**
     * 初始化监听
     */
    protected open fun initListener() {
    }

    /**
     * 线程安全的提示信息
     */
    protected fun toasts(message: String){
        runOnUiThread { toast(message) }
    }
}